<?php
defined ( 'IN_TS' ) or die ( 'Access Denied.' );

// 用户是否登录
$userid = aac ( 'user' )->isLogin ();

//普通不用不允许编辑内容
if($TS_SITE['isallowedit'] && $TS_USER ['isadmin'] == 0) tsNotice('系统不允许用户编辑内容，请联系管理员编辑！');

switch ($ts) {
	
	case "" :
		
		$articleid = intval ( $_GET ['articleid'] );
		
		$groupid = intval ( $_GET ['groupid'] );
		
		$strArticle = $new ['article']->find ( 'article', array (
				'articleid' => $articleid 
		) );

		if ($strArticle ['addtime'] < strtotime('-7 day') && $TS_USER ['isadmin'] == 0) {
			tsNotice('超过一周，不允许编辑，如需代为编辑请给微澜图书馆公众号后台留言');
		}
		
		if ($strArticle ['userid'] == $userid || $TS_USER ['isadmin'] == 1) {


			$strArticle['title'] = stripslashes($strArticle['title']);
			$strArticle['content'] = tsDecode($strArticle['content']);
			
			// 找出TAG
			$arrTags = aac ( 'tag' )->getObjTagByObjid ( 'article', 'articleid', $articleid );
			foreach ( $arrTags as $key => $item ) {
				$arrTag [] = $item ['tagname'];
			}
			$strArticle ['tag'] = arr2str ( $arrTag );
			//$strArticle ['content'] = replace(addslashes($strArticle ['content']));
			
			$title = '修改文档';
			include template ( 'edit' );
		} else {
			
			tsNotice ( '非法操作！' );
		}
		
		break;
	
	case "do" :
		
		$articleid = intval ( $_POST ['articleid'] );
		
		$strArticle = $new ['article']->find ( 'article', array (
				'articleid' => $articleid 
		) );
		
		if($strArticle['userid']!=$userid && $TS_USER['isadmin']==0){
			tsNotice('非法操作！');
		}
		
		$groupid = intval ( $_POST ['groupid'] );
		$title = trim ( $_POST ['title'] );
		$content = tsClean ( $_POST ['content'] );
		$isnotice = intval($_POST['isnotice']);
		$url =  trim($_POST['url']);

		if ($TS_USER ['isadmin'] == 0) {
			// 过滤内容开始
			aac ( 'system' )->antiWord ( $title );
			aac ( 'system' )->antiWord ( $content );
			// 过滤内容结束
		}


		if ($isnotice == ''){
			$isnotice =0;
		}

		if ($url){
			if(!$title){
				tsNotice("发布外链时标题不能为空！");
			}

		}

		if($url){
			$content ='';
		}
		if ($content == ''&& $url==''){
			tsNotice("内容和外链不能同时为空！");
		}


		$new ['article']->update ( 'article', array (
			'articleid' => $articleid 
		), array (		
			'groupid' => $groupid,
			'title' => $title,
			'isnotice' => $isnotice,
			'content' => $content ,
			'url' => $url,
			'uptime' => time(),
		));

		$img_path = getTextPhotos(tsDecode($content));
		$count = count($img_path);
		$img_path = $img_path[$count-1];

		// 上传图片开始
		if($img_path){
			$arrUpload = tsUploadPhotoUrl($img_path, $articleid, 'article', array('jpg', 'gif', 'png', 'jpeg'));
			if ($arrUpload) {
				$new['article'] -> update('article', array(
					'articleid' => $articleid
				), array(
					'path' => $arrUpload['path'],
					'photo' => $arrUpload['url']
				));
			}
			// 上传图片结束
		}

		// 处理标签
		$tag = trim ( $_POST ['tag'] );
		if ($tag) {
			aac ( 'tag' )->delIndextag ( 'article', 'articleid', $articleid );
			aac ( 'tag' )->addTag ( 'article', 'articleid', $articleid, $tag );
		}



		header ( "Location: " . tsUrl ( 'article', 'show', array (
				'id' => $articleid 
		) ) );
		
		break;
}